//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CommodityUnitCost.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Money.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (Cephei::QL::IMoney^ amount, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ unitOfMeasure) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMoney^ _Camount;
    CUnitOfMeasure^ _CunitOfMeasure;
    try
    {
#ifdef HANDLE
        _phCommodityUnitCost = NULL;
#endif
        _Camount = safe_cast<CMoney^> (amount);
        _Camount->Lock();
        QuantLib::Money& _amount = static_cast<QuantLib::Money&> (_Camount->GetReference ()); 
        _CunitOfMeasure = safe_cast<CUnitOfMeasure^> (unitOfMeasure);
        _CunitOfMeasure->Lock();
        QuantLib::UnitOfMeasure& _unitOfMeasure = static_cast<QuantLib::UnitOfMeasure&> (_CunitOfMeasure->GetReference ()); 
        _ppCommodityUnitCost = new boost::shared_ptr<QuantLib::CommodityUnitCost> (new QuantLib::CommodityUnitCost ( _amount,  _unitOfMeasure ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Camount != nullptr) _Camount->Unlock();
        if (_CunitOfMeasure != nullptr) _CunitOfMeasure->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCommodityUnitCost = NULL;
#endif
        _ppCommodityUnitCost = new boost::shared_ptr<QuantLib::CommodityUnitCost> (new QuantLib::CommodityUnitCost ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (boost::shared_ptr<QuantLib::CommodityUnitCost>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityUnitCost = NULL;
#endif
	_ppCommodityUnitCost = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (QuantLib::CommodityUnitCost& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCommodityUnitCost = NULL;
#endif
	_ppCommodityUnitCost = new boost::shared_ptr<QuantLib::CommodityUnitCost> (&childNative);
    
    _CommodityUnitCostOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (CCommodityUnitCost^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityUnitCost = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommodityUnitCost = new boost::shared_ptr<QuantLib::CommodityUnitCost> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityUnitCost = NULL;
#endif
	if (!t->IsSubclassOf(CCommodityUnitCost::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (QuantLib::Handle<QuantLib::CommodityUnitCost>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityUnitCost = &childNative;
	_ppCommodityUnitCost = &static_cast<boost::shared_ptr<QuantLib::CommodityUnitCost>>(childNative.currentLink());
    
    _CommodityUnitCostOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (QuantLib::Handle<QuantLib::CommodityUnitCost> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityUnitCost = &childNative;
	_ppCommodityUnitCost = &static_cast<boost::shared_ptr<QuantLib::CommodityUnitCost>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::CCommodityUnitCost (QuantLib::CommodityUnitCost childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityUnitCost = NULL;
#endif
	_ppCommodityUnitCost = new boost::shared_ptr<QuantLib::CommodityUnitCost> (new QuantLib::CommodityUnitCost (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CCommodityUnitCost::~CCommodityUnitCost ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCommodityUnitCost != NULL)
    {
	    delete _ppCommodityUnitCost;
        _ppCommodityUnitCost = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommodityUnitCost::!CCommodityUnitCost ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCommodityUnitCost != NULL)
    {
	    delete _ppCommodityUnitCost;
    }
}
QuantLib::CommodityUnitCost& Cephei::QL::Experimental::Commodities::CCommodityUnitCost::GetReference ()
{
    if (_ppCommodityUnitCost == NULL) throw REFNEW NativeNullException ();
	return **_ppCommodityUnitCost;
}
boost::shared_ptr<QuantLib::CommodityUnitCost>& Cephei::QL::Experimental::Commodities::CCommodityUnitCost::GetShared ()
{
    if (_ppCommodityUnitCost == NULL) throw REFNEW NativeNullException ();
	return *_ppCommodityUnitCost;
}
QuantLib::CommodityUnitCost* Cephei::QL::Experimental::Commodities::CCommodityUnitCost::GetPointer ()
{
    if (_ppCommodityUnitCost == NULL) throw REFNEW NativeNullException ();
	return &**_ppCommodityUnitCost;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CommodityUnitCost>& Cephei::QL::Experimental::Commodities::CCommodityUnitCost::GetHandle ()
{
	if (_phCommodityUnitCost == NULL)
	{
		_phCommodityUnitCost = new Handle<QuantLib::CommodityUnitCost> (*_ppCommodityUnitCost);
	}
	return *_phCommodityUnitCost;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommodityUnitCost::HasNative () 
{
	return (_ppCommodityUnitCost != NULL);
}

Cephei::QL::IMoney^ Cephei::QL::Experimental::Commodities::CCommodityUnitCost::Amount::get ()
{
    try
    {
    	QuantLib::Money& _rv = (QuantLib::Money&)(*_ppCommodityUnitCost)->amount ( );   
        Cephei::QL::CMoney^ _nrv = REFNEW Cephei::QL::CMoney (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CCommodityUnitCost::UnitOfMeasure::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppCommodityUnitCost)->unitOfMeasure ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::ICommodityUnitCost^ Cephei::QL::Experimental::Commodities::CCommodityUnitCost_Factory::Create (Cephei::QL::IMoney^ amount, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ unitOfMeasure)
{
    return REFNEW CCommodityUnitCost ( amount,  unitOfMeasure);
}
Cephei::QL::Experimental::Commodities::ICommodityUnitCost^ Cephei::QL::Experimental::Commodities::CCommodityUnitCost_Factory::Create ()
{
    return REFNEW CCommodityUnitCost ();
}
